//
//  YMMessageViewController.swift
//  Driver
//
//  Created by lantaiyuan on 2017/10/25.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit

class YMMessageViewController: UITableViewController {

    var datas:[YMMessage_Obj] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        // 注册原型 cell
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        
        let path = Bundle.main.path(forResource: "message.json", ofType: nil) ?? ""
        
        do {
            guard let data = try? Data.init(contentsOf: URL(fileURLWithPath: path)),
                let dic = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: Any] else{
                return
            }
            print(dic)
            
            let msg = YMMessage.model(withJSON: dic) ?? YMMessage()
            datas = msg.obj
            
            print(msg)
            
            tableView.reloadData()
            
        } catch {
        }
    }
}

// MARK: - Table view data source
extension YMMessageViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "msgCell", for: indexPath) as! YMMessageCell
        
        let msg:YMMessage_Obj = datas[indexPath.row]
        
        cell.model = msg
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 180.0
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       tableView.deselectRow(at: indexPath, animated: true)
    }
}
